home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 1 / your choice.zip / your choice / PRGMMING / DFLT18 / DECOMP.C < prev    next >
C/C++ Source or Header  |  1994-02-25  |  3KB  |  109 lines

  1. /* ------------------- decomp.c -------------------- */
  2.  
  3. /*
  4.  * Decompress the application.HLP file
  5.  * or load the application.TXT file if the .HLP file
  6.  * does not exist
  7.  */
  8.  
  9. #include "dflat.h"
  10. #include "htree.h"
  11.  
  12. static int in8;
  13. static int ct8 = 8;
  14. static FILE *fi;
  15. static BYTECOUNTER bytectr;
  16. struct htr *HelpTree;
  17. static int root;
  18.  
  19. /* ------- open the help database file -------- */
  20. FILE *OpenHelpFile(void)
  21. {
  22.     char *cp;
  23.     int treect, i;
  24.     char helpname[65];
  25.  
  26.     /* -------- get the name of the help file ---------- */
  27.     BuildFileName(helpname, ".hlp");
  28.     if ((fi = fopen(helpname, "r+b")) == NULL)
  29.         return NULL;
  30.  
  31.     /* ----- read the byte count ------ */
  32.     fread(&bytectr, sizeof bytectr, 1, fi);
  33.     /* ----- read the frequency count ------ */
  34.     fread(&treect, sizeof treect, 1, fi);
  35.     /* ----- read the root offset ------ */
  36.     fread(&root, sizeof root, 1, fi);
  37.     HelpTree = calloc(treect-256, sizeof(struct htr));
  38.     if (HelpTree != NULL)    {
  39.         /* ---- read in the tree --- */
  40.         for (i = 0; i < treect-256; i++)    {
  41.             fread(&HelpTree[i].left,  sizeof(int), 1, fi);
  42.             fread(&HelpTree[i].right, sizeof(int), 1, fi);
  43.         }
  44.     }
  45.     return fi;
  46. }
  47.  
  48. /* ----- read a line of text from the help database ----- */
  49. void *GetHelpLine(char *line)
  50. {
  51.     int h;
  52.     *line = '\0';
  53.     while (TRUE)    {
  54.         /* ----- decompress a line from the file ------ */
  55.         h = root;
  56.         /* ----- walk the Huffman tree ----- */
  57.         while (h > 255)    {
  58.             /* --- h is a node pointer --- */
  59.             if (ct8 == 8)   {
  60.                 /* --- read 8 bits of compressed data --- */
  61.                 if ((in8 = fgetc(fi)) == EOF)    {
  62.                     *line = '\0';
  63.                     return NULL;
  64.                 }
  65.                 ct8 = 0;
  66.             }
  67.             /* -- point to left or right node based on msb -- */
  68.             if (in8 & 0x80)
  69.                 h = HelpTree[h-256].left;
  70.             else
  71.                 h = HelpTree[h-256].right;
  72.             /* --- shift the next bit in --- */
  73.             in8 <<= 1;
  74.             ct8++;
  75.         }
  76.         /* --- h < 255 = decompressed character --- */
  77.         if (h == '\r')
  78.             continue;    /* skip the '\r' character */
  79.         /* --- put the character in the buffer --- */
  80.         *line++ = h;
  81.         /* --- if '\n', end of line --- */
  82.         if (h == '\n')
  83.             break;
  84.     }
  85.     *line = '\0';    /* null-terminate the line */
  86.     return line;
  87. }
  88.  
  89. /* --- compute the database file byte and bit position --- */
  90. void HelpFilePosition(long *offset, int *bit)
  91. {
  92.     *offset = ftell(fi);
  93.     if (ct8 < 8)
  94.         --*offset;
  95.     *bit = ct8;
  96. }
  97.  
  98. /* -- position the database to the specified byte and bit -- */
  99. void SeekHelpLine(long offset, int bit)
  100. {
  101.     fseek(fi, offset, 0);
  102.     ct8 = bit;
  103.     if (ct8 < 8)    {
  104.         in8 = fgetc(fi);
  105.         in8 <<= bit;
  106.     }
  107. }
  108.  
  109.